//
//  ZZLightLineTool.m
//  ZZBlue
//
//  Created by evan on 2017/10/16.
//  Copyright © 2017年 stonesMonkey. All rights reserved.
//

#import "ZZLightLineTool.h"
#import "ZZLightLineGroupModel.h"
#import "ZZLightLineModel.h"

#define kLightLineModelKey @"LightLineModelKey"
#define kLightLinePath @"LightLinePath.plist"

@implementation ZZLightLineTool

+ (instancetype)allocWithZone:(struct _NSZone *)zone {

    static id listGroupModel = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{

        listGroupModel = [super allocWithZone:zone];

    });

    return listGroupModel;
}


- (void)save {

    //准备路径:
    NSString *path = NSHomeDirectory();
    NSLog(@"%@",path);

    path = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/%@",kLightLinePath]];
    //1:准备存储数据的对象
    NSMutableData *data = [NSMutableData data];
    //2:创建归档对象
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    //3:开始归档
    [archiver encodeObject:self.groupModel forKey:kLightLineModelKey];
    //4:完成归档
    [archiver finishEncoding];
    //5:写入文件当中
    BOOL result = [data writeToFile:path atomically:YES];
    if (result) {

        // 写入成功
    }
}

- (ZZLightLineGroupModel *)groupModel {

    if (!_groupModel) {

        // 从数据中拿
        NSString *path = NSHomeDirectory();
        path = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/%@",kLightLinePath]];
        NSData *myData = [NSData dataWithContentsOfFile:path];
        //创建反归档对象
        NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:myData];
        //反归档
        ZZLightLineGroupModel* listGroupModel = [unarchiver decodeObjectForKey:kLightLineModelKey];
        //完成反归档
        [unarchiver finishDecoding];

        if (listGroupModel == nil) {

            listGroupModel = [ZZLightLineGroupModel new];

            ZZLightLineModel *model1 = [ZZLightLineModel new];
            model1.title = @"自定义";
            model1.model = LightLineModelCustom;
            model1.colorZuHeShu = 5;
            model1.pinLv = 0.5;
            model1.lightValue = 0.5;
            for (NSInteger index = 0; index < 5; index++) {

                [model1.colorsGroup addObject:[NSMutableArray arrayWithArray:@[[ZZColorRGBA colorRGBAWithR:255.0 G:0 B:0 A:1],[ZZColorRGBA colorRGBAWithR:0 G:255.0 B:0 A:1],[ZZColorRGBA colorRGBAWithR:0 G:0 B:255.0 A:1]]]];
            }


            [model1 reloadImage];

            ZZLightLineModel *model2 = [ZZLightLineModel new];
            model2.title = @"激光跑马";
            model2.model = LightLineModelLiuXing;


            for (NSInteger index = 0; index < 5; index++) {

                [model2.colorsGroup addObject:[NSMutableArray arrayWithArray:@[[ZZColorRGBA colorRGBAWithR:255.0 G:0 B:0 A:1],[ZZColorRGBA colorRGBAWithR:0 G:255.0 B:0 A:1],[ZZColorRGBA colorRGBAWithR:0 G:0 B:255.0 A:1]]]];
            }

            [model2 reloadImage];

            ZZLightLineModel *model3 = [ZZLightLineModel new];
            model3.title = @"梦幻一夏（颜色跳变）";
            model3.model = LightLineModelTiaoBian;

            model3.colorZuHeShu = 5;
            for (NSInteger index = 0; index < 5; index++) {

                [model3.colorsGroup addObject:[NSMutableArray arrayWithArray:@[[ZZColorRGBA colorRGBAWithR:255.0 G:255.0 B:0 A:1],[ZZColorRGBA colorRGBAWithR:0 G:255.0 B:0 A:1],[ZZColorRGBA colorRGBAWithR:255.0 G:0 B:0 A:1]]]];
            }


            [model3 reloadImage];

            ZZLightLineModel *model4 = [ZZLightLineModel new];
            model4.title = @"光线覆盖";
            model4.model = LightLineModelFugai;
            model4.colorZuHeShu = 5;

            model4.imageSize = CGSizeMake(130, 130);
            for (NSInteger index = 0; index < 5; index++) {

                [model4.colorsGroup addObject:[NSMutableArray arrayWithArray:@[[ZZColorRGBA colorRGBAWithR:0 G:255.0 B:255.0 A:1],[ZZColorRGBA colorRGBAWithR:0 G:255.0 B:0 A:1],[ZZColorRGBA colorRGBAWithR:255.0 G:0 B:0 A:1]]]];
            }
            [model4 reloadImage];

            [listGroupModel.models addObject:model1];
            [listGroupModel.models addObject:model2];
            [listGroupModel.models addObject:model3];
            [listGroupModel.models addObject:model4];

            {
                ZZLightLineModel *model1 = [ZZLightLineModel new];
                model1.title = @"梦幻叠加";
                model1.model = LightLineModelDieJia;

                for (NSInteger index = 0; index < 5; index++) {

                    model1.colorsGroup[index] = [NSMutableArray arrayWithArray:@[[ZZColorRGBA colorRGBAWithR:255.0 G:255.0 B:0 A:1],[ZZColorRGBA colorRGBAWithR:0 G:255.0 B:0 A:1],[ZZColorRGBA colorRGBAWithR:255.0 G:0 B:0 A:1]]];
                }
                model1.colorZuHeShu =5;
                model1.imageSize = CGSizeMake(130, 130);
                [model1 reloadImage];

                ZZLightLineModel *model2 = [ZZLightLineModel new];
                model2.title = @"间隔";
                model2.model = LightLineModelJianGe;
                model2.dicrection = LightLineDirectionCenter;
                for (NSInteger index = 0; index < 5; index++) {

                    [model2.colorsGroup addObject:[NSMutableArray arrayWithArray:@[[ZZColorRGBA colorRGBAWithR:255.0 G:0 B:255.0 A:1],[ZZColorRGBA colorRGBAWithR:0 G:255.0 B:0 A:1],[ZZColorRGBA colorRGBAWithR:255.0 G:0 B:0 A:1]]]];
                }

                [model2 reloadImage];

                [listGroupModel.models addObject:model1];
                [listGroupModel.models addObject:model2];
            }

            _groupModel = listGroupModel;
             [self save];
        }

        _groupModel = listGroupModel;

    }

    return _groupModel;
}
@end
